//
//  MyScollview.m
//  UIliudongzimo
//
//  Created by 元虎的mac on 15/8/26.
//  Copyright (c) 2015年 杨元虎. All rights reserved.
//

#import "MyScollview.h"

@implementation MyScollview
- (instancetype)initWithFrame:(CGRect)frame str:(NSString *)Str
{
    self = [super initWithFrame:frame];
    if (self) {//此处计算的是高度
        [self returnStringHeight:Str Wdth:frame.size.height Font:17];
        _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _wdth, frame.size.height)];
        _title.text = Str;
        _title.numberOfLines = 0;
        _title.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_title];
        self.contentSize = CGSizeMake(_wdth, frame.size.height);
        //创建定时器
        self.time = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self selector:@selector(overtime:) userInfo:nil repeats:YES];
        self.tmeer = 0;
        [self.time fire];
    

    }
    return self;
}

- (CGFloat)returnStringHeight:(NSString *)string Wdth:(CGFloat)Wdth Font:(int)font{
    CGRect frame = [string boundingRectWithSize:CGSizeMake(10000, Wdth) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName] context:nil];
    self.wdth = frame.size.width;
    return frame.size.width;
}
- (void)overtime:(NSTimer *)time{
    //定时器每隔一段时间调用这个方法
    //我们设置一个数来进行增加和减少 并控制滚动的偏移距离   >50时让视图显示时等待一段时间后开始流动
 

    self.tmeer++;
    //if (_wdth>self.frame.size.width&&_tmeer>50) {
    self.contentOffset = CGPointMake(self.tmeer, 0);
        if (self.tmeer>self.wdth -20) {//如果偏移距离超过需要的宽的我们把它重新开始滚动
            self.tmeer=0;
            self.contentOffset = CGPointMake(self.tmeer, 0);
       // }
    }
    //self.contentOffset = CGPointMake(self.tmeer, 0);
    
}


@end
